<template>
  <li @mouseenter="moveTodo(true)" @mouseleave="moveTodo(false)"
  :style="{background : backC}">
    <label>
      <input type="checkbox" v-model="isCheck"/>
      <span>{{todo.title}}</span>
    </label>
    <button class="btn btn-danger" v-show="isShow" @click="confirmDelete">删除</button>
  </li>
</template>

<script>
export default {
  name: 'Item',
  props:{
    todo:Object,
    checkTodo:Function,
    index:Number,
  },
  data(){
    return{
      isShow: false,
      backC:'#fff',
    }
  },
  methods:{
    moveTodo(isEnter){
      if(isEnter){
        this.isShow = true;
        this.backC = '#ccc'
      }else{
        this.isShow = false;
        this.backC = '#fff';
      }
    },
    confirmDelete(){
      const {index,todo:{title}} = this;
      if(confirm(`确定要删除${title}吗？`)){
        // deleteTodo(index);
        this.$globaleventBus.$emit('deleteTodo', index)
      }
    }
  },
  computed:{
    isCheck:{
      // 确定多选框是否选中
      get(){
        return this.todo.completed;
      },
      set(value){
        console.log("set",value);
        // 监视属性是否发生变化
        this.checkTodo(this.todo);// 调用父组件传入的函数来修改父组件的数据
      }
    }
  }
}
</script>

<style scoped>
  li {
    list-style: none;
    height: 36px;
    line-height: 36px;
    padding: 0 5px;
    border-bottom: 1px solid #ddd;
  }

  li label {
    float: left;
    cursor: pointer;
  }

  li label li input {
    vertical-align: middle;
    margin-right: 6px;
    position: relative;
    top: -1px;
  }

  li button {
    float: right;
    /* display: none; */
    margin-top: 3px;
  }

  li:before {
    content: initial;
  }

  li:last-child {
    border-bottom: none;
  }
</style>